package atmgui;

import atm.FileUtil;
import atm.User;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class ATM05 extends JFrame {
    private ArrayList users;

    private  User loginUser;

public static final String FILE_PATH  = "users.date";



    private Image bgImg = new ImageIcon("src/images/background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private SavePanel savePanel=new SavePanel(this);
    private GetPanel getPanel=new GetPanel(this);
    private  SearchPanel searchPanel=new SearchPanel(this);
    private TransferPanel transferPanel=new TransferPanel(this);
    private  ChangePwdPanel changePwdPanel=new ChangePwdPanel(this);




    public ATM05(){
        this.in();

        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });
        this.setLayout(cardLayout);


        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(savePanel,"save");
        this.add(getPanel,"get");
        this.add(searchPanel,"search");
        this.add(transferPanel,"transfer");
        this.add(changePwdPanel,"changePwd");

        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }



    public void in() {
        users = (ArrayList) FileUtil.readObject(FILE_PATH);
        if (users == null) {
            users = new ArrayList<>();
            users.add(new User("zyd1", "1", 1000 ));
            users.add(new User("zyd2", "2", 2000));
            users.add(new User("zyd3", "3", 3000));
            users.add(new User("zyd4", "4", 4000));
            users.add(new User("zyd5", "5", 5000));
            FileUtil.writeObject(users, FILE_PATH);

        }
    }

    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }


    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public void setSearchPanel(SearchPanel searchPanel) {
        this.searchPanel = searchPanel;
    }

    public void showPanel(String name){

        cardLayout.show(this.getContentPane(),name);
    }
    public static void main(String[] args) {

        new ATM05();
    }

    public ArrayList getUsers() {
        return users;
    }
}
